package com.cspg.design.request;

import com.cspg.design.common.Constant;
import com.cspg.design.dto.MyHighVoltageSchemeDto;
import com.cspg.design.dto.MyLowVoltageSchemeDto;
import com.cspg.design.dto.ProjectDto;
import com.cspg.design.http.IService;
import com.cspg.design.http.RetrofitUtil;
import com.cspg.design.model.MyHighVoltageSchemeModel;
import com.cspg.design.model.MyLowVoltageSchemeModel;
import com.cspg.design.presenter.GTaskPresenter;
import com.cspg.design.presenter.SchemePresenter;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class Scheme {
    private Call call;
    private final SchemePresenter presenter;

    public Scheme(SchemePresenter presenter) {
        this.presenter = presenter;
    }

    public void updateHighVoltageScheme(MyHighVoltageSchemeDto myHighVoltageSchemeDto) throws Exception
    {
        RetrofitUtil retrofitUtil = new RetrofitUtil();
        call = retrofitUtil.createReq(IService.class).updateMyHighVoltageScheme(myHighVoltageSchemeDto);
        call.enqueue(new Callback() {
            @Override
            public void onResponse(Call call, Response response) {
                if (null == response) {
                    presenter.getFailure(Constant.HTTP_NO_NET_WORK);
                    return;
                }
                if (response.code() == 401)
                {
                    presenter.getTokenExpireFailure();
                    return;
                }

                if (response.code() == 204)
                {
                    presenter.updateHighSuccess();
                    return;
                }

                if (!response.isSuccessful()) {
                    presenter.getFailure(Constant.HTTP_NO_NET_WORK);
                    return;
                }
                Object obj = response.body();
                MyHighVoltageSchemeDto highVoltageSchemeDto= (MyHighVoltageSchemeDto) response.body();
                if (null == highVoltageSchemeDto) {
                    presenter.getFailure(Constant.HTTP_REQUEST_NET_WORK_ERROR);
                    return;
                }
                else
                {
                    presenter.requestHighSuccess(highVoltageSchemeDto);
                }
            }
            @Override
            public void onFailure(Call call, Throwable t) {
                presenter.getFailure(Constant.HTTP_NO_NET_WORK);
            }
        });
    }
    public void updateLowVoltageScheme(MyLowVoltageSchemeDto myLowVoltageSchemeDto) throws Exception
    {
        RetrofitUtil retrofitUtil = new RetrofitUtil();
        call = retrofitUtil.createReq(IService.class).updateMyLowVoltageScheme(myLowVoltageSchemeDto);
        call.enqueue(new Callback() {
            @Override
            public void onResponse(Call call, Response response) {
                if (null == response) {
                    presenter.getFailure(Constant.HTTP_NO_NET_WORK);
                    return;
                }
                if (response.code() == 401)
                {
                    presenter.getTokenExpireFailure();
                    return;
                }

                if (response.code() == 204)
                {
                    presenter.updateLowSuccess();
                    return;
                }

                if (!response.isSuccessful()) {
                    presenter.getFailure(Constant.HTTP_NO_NET_WORK);
                    return;
                }
                Object obj = response.body();
                MyLowVoltageSchemeDto lowVoltageSchemeDto= (MyLowVoltageSchemeDto) response.body();
                if (null == lowVoltageSchemeDto) {
                    presenter.getFailure(Constant.HTTP_REQUEST_NET_WORK_ERROR);
                    return;
                }
                else
                {
                    presenter.requestLowSuccess(lowVoltageSchemeDto);
                }
            }
            @Override
            public void onFailure(Call call, Throwable t) {
                presenter.getFailure(Constant.HTTP_NO_NET_WORK);
            }
        });
    }

    public void addHighVoltageScheme(MyHighVoltageSchemeDto myHighVoltageSchemeDto) throws Exception
    {
        RetrofitUtil retrofitUtil = new RetrofitUtil();
        call = retrofitUtil.createReq(IService.class).addMyHighVoltageScheme(myHighVoltageSchemeDto);
        call.enqueue(new Callback() {
            @Override
            public void onResponse(Call call, Response response) {
                if (null == response) {
                    presenter.getFailure(Constant.HTTP_NO_NET_WORK);
                    return;
                }
                if (response.code() == 401)
                {
                    presenter.getTokenExpireFailure();
                    return;
                }

                if (response.code() == 204)
                {
                    presenter.updateHighSuccess();
                }

                if (response.code() == 208)
                {
                    presenter.getFailure("方案名称已经存在，请重新输入。。。。");
                    return;
                }

                if (!response.isSuccessful()) {
                    presenter.getFailure(Constant.HTTP_NO_NET_WORK);
                    return;
                }
                Object obj = response.body();
                MyHighVoltageSchemeDto highVoltageSchemeDto= (MyHighVoltageSchemeDto) response.body();
                if (null == highVoltageSchemeDto) {
                    presenter.getFailure(Constant.HTTP_REQUEST_NET_WORK_ERROR);
                    return;
                }
                else
                {
                    presenter.requestHighSuccess(highVoltageSchemeDto);
                }
            }
            @Override
            public void onFailure(Call call, Throwable t) {
                presenter.getFailure(Constant.HTTP_NO_NET_WORK);
            }
        });
    }
    public void addLowVoltageScheme(MyLowVoltageSchemeDto myLowVoltageSchemeDto) throws Exception
    {
        RetrofitUtil retrofitUtil = new RetrofitUtil();
        call = retrofitUtil.createReq(IService.class).addMyLowVoltageScheme(myLowVoltageSchemeDto);
        call.enqueue(new Callback() {
            @Override
            public void onResponse(Call call, Response response) {
                if (null == response) {
                    presenter.getFailure(Constant.HTTP_NO_NET_WORK);
                    return;
                }
                if (response.code() == 401)
                {
                    presenter.getTokenExpireFailure();
                    return;
                }

                if (response.code() == 204)
                {
                    presenter.updateLowSuccess();
                }

                if (response.code() == 208)
                {
                    presenter.getFailure("方案名称已经存在，请重新输入。。。。");
                    return;
                }

                if (!response.isSuccessful()) {
                    presenter.getFailure(Constant.HTTP_NO_NET_WORK);
                    return;
                }
                Object obj = response.body();
                MyLowVoltageSchemeDto lowVoltageSchemeDto= (MyLowVoltageSchemeDto) response.body();
                if (null == lowVoltageSchemeDto) {
                    presenter.getFailure(Constant.HTTP_REQUEST_NET_WORK_ERROR);
                    return;
                }
                else
                {
                    presenter.requestLowSuccess(lowVoltageSchemeDto);
                }
            }
            @Override
            public void onFailure(Call call, Throwable t) {
                presenter.getFailure(Constant.HTTP_NO_NET_WORK);
            }
        });
    }
    public void getHighVoltageScheme() throws Exception
    {
        RetrofitUtil retrofitUtil = new RetrofitUtil();
        call = retrofitUtil.createReq(IService.class).getMyHighVoltageScheme(0,100000);
        call.enqueue(new Callback() {
            @Override
            public void onResponse(Call call, Response response) {
                if (null == response) {
                    presenter.getFailure(Constant.HTTP_NO_NET_WORK);
                    return;
                }
                if (response.code() == 401)
                {
                    presenter.getTokenExpireFailure();
                    return;
                }

                if (response.code() == 204)
                {
                    presenter.updateLowSuccess();
                }

                if (!response.isSuccessful()) {
                    presenter.getFailure(Constant.HTTP_NO_NET_WORK);
                    return;
                }
                Object obj = response.body();
                MyHighVoltageSchemeModel myHighVoltageSchemeModel= (MyHighVoltageSchemeModel) response.body();
                if (null == myHighVoltageSchemeModel) {
                    presenter.getFailure(Constant.HTTP_REQUEST_NET_WORK_ERROR);
                    return;
                }
                else
                {
                    presenter.requestHighSuccess(myHighVoltageSchemeModel.getContent());
                }
            }
            @Override
            public void onFailure(Call call, Throwable t) {
                presenter.getFailure(Constant.HTTP_NO_NET_WORK);
            }
        });
    }
    public void getLowVoltageScheme() throws Exception
    {
        RetrofitUtil retrofitUtil = new RetrofitUtil();
        call = retrofitUtil.createReq(IService.class).getMyLowVoltageScheme(0,100000);
        call.enqueue(new Callback() {
            @Override
            public void onResponse(Call call, Response response) {
                if (null == response) {
                    presenter.getFailure(Constant.HTTP_NO_NET_WORK);
                    return;
                }
                if (response.code() == 401)
                {
                    presenter.getTokenExpireFailure();
                    return;
                }

                if (response.code() == 204)
                {
                    presenter.updateLowSuccess();
                }

                if (!response.isSuccessful()) {
                    presenter.getFailure(Constant.HTTP_NO_NET_WORK);
                    return;
                }
                Object obj = response.body();
                MyLowVoltageSchemeModel myLowVoltageSchemeModel= (MyLowVoltageSchemeModel) response.body();
                if (null == myLowVoltageSchemeModel) {
                    presenter.getFailure(Constant.HTTP_REQUEST_NET_WORK_ERROR);
                    return;
                }
                else
                {
                    presenter.requestLowSuccess(myLowVoltageSchemeModel.getContent());
                }
            }
            @Override
            public void onFailure(Call call, Throwable t) {
                presenter.getFailure(Constant.HTTP_NO_NET_WORK);
            }
        });
    }
}

